

/*
 * @Autor: xgq
 * @Description: 
 * @Version: 1.0
 * @Date: 2020-05-12 10:04:53
 */
// var fs = require('fs')
var path = require('path')
var express = require('express');
var special_router = express.Router();
var db = require('../../models');
var comm = require('../../comm')

special_router.post('/addHotTopics', async (req, res) => {
  let oldthumbnail = req.body.thumbnail
  let thumbnail = oldthumbnail.replace('temp_files/', '')
  var newthumbnail = `special_topic/${thumbnail}`
  var sourceFile = path.join(process.cwd(), 'static/temp_files', thumbnail);
  var targetPath = path.join(process.cwd(), 'static/images', newthumbnail);
  let moveed = await comm.move_image(sourceFile,targetPath)
  if( moveed !== 'error' ){
    var from=req.body
    from.thumbnail=newthumbnail
    let result = await db.special_topic.addHotTopics(from)
    res.json(result)
  }else{
    res.json(false)
  }
})


// 获取热门专题
special_router.get('/getHotTopics', async (req, res) => {
  var [page = 1, pagesize = 30] = [req.query.pageNo, req.query.pageSize]
  let condition = {
    "pagesize": pagesize,
    "page": page
  }
  let topic = await db.special_topic.getHotTopics(condition)
  let data = {
    code: 200,
    rows: topic
  }
  res.json(data)
})

// 获取热门专题
special_router.post('/getTopics', async (req, res) => {
  var [page = 1, pagesize = 30] = [req.query.pageNo, req.query.pageSize]
  let condition = {
    "pagesize": pagesize,
    "page": page
  }
  condition.status = 0
  let topic = await db.special_topic.getHotTopics(condition)
  let data = {
    code: 200,
    rows: topic
  }
  res.json(data)
})


// 修改状态
special_router.post('/modifyStatus', async (req, res) => {
  let pars = req.body
  let result = await db.special_topic.modifyStatus(pars);
  res.json(result)
})

// 修改专题
special_router.post('/editHotTopics', async (req, res) => {
  let thumbnail = req.body.thumbnail
  let id = req.body.id
  let special = await db.special_topic.getSpecialById(id)
  if(special.thumbnail!==thumbnail){
    comm.delete_imgage( path.join(process.cwd(), "static/images/", special.thumbnail)  )
    var sourceFile = path.join(process.cwd(), 'static', thumbnail);
    thumbnail = thumbnail.replace('temp_files/', 'special_topic/')
    var targetPath = path.join(process.cwd(), 'static/images',thumbnail);
    await comm.move_image(sourceFile,targetPath)      
  }
  let from = req.body
  from.thumbnail=thumbnail.replace('temp_files/', 'special_topic/')
  let result = await db.special_topic.editHotTopics(from);
  res.json(result)
})


special_router.post('/deleteHotTopic', async (req, res) => {
  var from = req.body
  var id = req.body.id
  let topic = await db.special_topic.getSpecialById(id)
  comm.delete_imgage( path.join(process.cwd(), "static/images/", topic.thumbnail))
  let result = await db.special_topic.deleteTopic(from)
  res.json(result)
})

//通过id或name搜索专题
special_router.post('/getTopicByIdOrName', async (req, res) => {
  let result = await db.special_topic.getTagByIdOrName(req.body.queryString)
  res.json(result)
})

//拖动排序
special_router.post('/hotSort', async (req, res) => {
  //start拖动的目的地
  //end从哪一行开始拖动的
  //start《end从后往前拖动   start》end从前往后拖动
  let result = await db.special_topic.hotSort(req.body.params)
  res.json(result)
})


// 获取列表条数，用于添加排序
special_router.get('/getHotCount', async (req, res) => {
  let result = await db.special_topic.getHotCount()
  res.json(result)
})


module.exports = special_router
